<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

// 引入TP的数据库类
use think\Db;

/**
 * 客户公海
 *
 * @icon fa fa-circle-o
 */
class Highsea extends Backend
{

    /**
     * Business模型对象
     * @var \app\common\model\Business\Highsea
     */
    protected $model = null;

    // 当前是否为关联查询
    protected $relationSearch = true;

    protected $dataLimit = 'false'; //默认基类中为false，表示不启用，可额外使用auth和personal两个值
    protected $dataLimitField = 'adminid'; //数据关联字段,当前控制器对应的模型表中必须存在该字段

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business.Business');
        $this->Visitmodel = model('Business.Visit');
        $this->Sourcemodel = model('Business.Source');
        $this->Receivemodel = model('Business.Receive');
        $this->Adminmodel = model('Business.Admin');
        $this->view->assign("genderList", $this->model->getGenderList());
        $this->view->assign("dealList", $this->model->getDealList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查询总数
            $total = $this->model
                ->where($where)
                ->count();

            //查询数据
            $list = $this->model
                ->with(['source', 'visit'])
                ->where(['business.adminid' =>  null])
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 详细
     */
    public function info($ids = 0)
    {
        $businlist = $this->model
            ->with(['source', 'visit'])
            ->where(['business.id' =>  $ids])
            ->find();

        $receive = $this->Receivemodel
            ->where(['busid' => $ids])
            ->select();

        // 赋值
        $this->view->assign([
            'businlist' => $businlist,
            'receive' => $receive,
        ]);
        return $this->view->fetch();
    }

    /**
     * 申请
     */
    public function apply($ids = 0)
    {
        if ($this->request->isAjax()) {
            $adminid = $this->auth->id;

            // 执行更新语句
            $result = Db::name('business')->where(['id' => ['in', $ids]])->update(['adminid' => $adminid]);

            $data = [
                'applyid' => $adminid,
                'busid' => $ids,
                'applytime' => time(),
                'status' => 'apply',
            ];

            $apply = $this->Receivemodel->save($data);

            // 判断是否申请成功
            if ($result) {
                $this->success();
                exit;
            } else {
                $this->error(__('申请失败'));
                exit;
            }

            // 判断是否领取成功
            if ($apply) {
                $this->success();
                exit;
            } else {
                $this->error(__('领取失败'));
                exit;
            }
        }
    }

    /**
     * 分配
     */
    public function allot($ids = 0)
    {
        $admin = $this->Adminmodel->select();

        if($this->request->isPost())
        {
            $param = $this->request->param('row/a');

            $param = implode(',',$param);

            $adminid = Db::name('admin')->where('nickname',$param)->find();

            $adminid = $adminid['id'];

            // 执行更新语句
            $result = Db::name('business')->where(['id' => ['in', $ids]])->update(['adminid' => $adminid]);

            $data = [
                'applyid' => $adminid,
                'busid' => $ids,
                'applytime' => time(),
                'status' => 'allot',
            ];

            $allot = $this->Receivemodel->save($data);

            // 判断是否分配成功
            if ($result) {
                $this->success();
                exit;
            } else {
                $this->error(__('分配失败'));
                exit;
            }

            // 判断领取表是否更新成功
            if ($allot) {
                $this->success();
                exit;
            } else {
                $this->error(__('领取表更新失败'));
                exit;
            }
        }

        // 赋值
        $this->view->assign([
            'admin' => $admin,
        ]);
        return $this->view->fetch();
    }

    /**
     * 软删除方法
     */
    public function del($ids = 0)
    {
        // where in 语句 列表查询
        $rows = $this->model->where(['id' => ['in', $ids]])->select();

        if (!$rows) {
            $this->error('暂无数据');
            exit;
        }

        // 直接软删除
        $result = $this->model->destroy($ids);

        if ($result === FALSE) {
            $this->error('删除用户失败');
            exit;
        } else {
            $this->success();
            exit;
        }
    }
}
